package cn.iune.erp.inventory;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Aspect//指定这是一个其切面
public class StorageRecordDetailDaoAspect {
	
	@Autowired
	private IInventoryService storageService;
	
	//指定切入点（增加）
    @Pointcut("execution(* cn.iune.erp.inventory.IStorageRecordDetailDao.insert(..)) && args(obj)")
    public void insertPointCut(StorageRecordDetail obj){}
    
    //指定通知以及通知的范围
    @Transactional
    @After("insertPointCut(obj)")
    public void afterInsert(StorageRecordDetail obj){
		//更新主表合计
		//插入
	
    	storageService.afterInsert(obj);
    }
    
	//指定切入点（删除）
    @Pointcut("execution(* cn.iune.erp.inventory.IStorageRecordDetailDao.delete(..)) && args(fID)")
    public void deletePointCut(String fID){}
    
    //指定通知以及通知的范围
    @Transactional
    @Before("deletePointCut(fID)")
    public void beforeDelete(String fID){
//		StorageRecordDetail obj = storageRecordDetailService.getByID(fID);
		//更新主表合计
    	
    	storageService.beforeDelete(fID);
    }
	//指定切入点（修改）
    @Pointcut("execution(* cn.iune.erp.inventory.IStorageRecordDetailDao.update(..)) && args(obj)")
    public void updatePointCut(StorageRecordDetail obj){}
    
    /*
    //指定通知以及通知的范围
    @Transactional
    @Before("updatePointCut(obj)")
    public void beforeUpdate(StorageRecordDetail obj){
		//更新主表合计
		//删除
//		this.beforeDelete(obj.getfID());
    	storageRecordDetailService.beforeDelete(obj.getfID());
		//插入
//		this.beforeInsert(obj);
    	storageRecordDetailService.afterInsert(obj);
    }
    */
    
	//指定通知以及通知的范围
	@Transactional
	@Around("updatePointCut(obj)")
	 public StorageRecordDetail aroundUpdate(JoinPoint joinPoint, StorageRecordDetail obj) throws Throwable{
	     System.out.println(this.getClass().getName()+"环绕通知上。。。");
		 storageService.beforeDelete(obj.getfID());
		 StorageRecordDetail objNew = (StorageRecordDetail) ((ProceedingJoinPoint) joinPoint).proceed();
		 System.out.println(this.getClass().getName()+"环绕通知下。。。");
		 storageService.afterInsert(obj);
		 return objNew;
	 }
    
	/*这是一个完整的例子
    //指定通知以及通知的范围
    @Before("insertPointCut(obj)")
    public void beforeInsert(StorageRecordDetail obj){
        System.out.println("前置通知。。。");
        System.out.println("@Before切切切切切切切切切切切切切切切切切切切切切切切"+obj.getfID());
    }

   @Around("insertPointCut(obj)")
    public void aroundInsert(JoinPoint joinPoint, StorageRecordDetail obj) throws Throwable{
        System.out.println("环绕通知上。。。");
		((ProceedingJoinPoint) joinPoint).proceed();
        System.out.println("@Around切切切切切切切切切切切切切切切切切切切切切切切"+obj.getfID());
        System.out.println("环绕通知下。。。");
    }

    @After("insertPointCut(obj)")
    public void afterInsert(StorageRecordDetail obj){
        System.out.println("后置通知。。。");
        System.out.println("@After切切切切切切切切切切切切切切切切切切切切切切切"+obj.getfID());
    }
	*/


}
